﻿// 2018/8/24: 首个记录
// 2019/3/29: 添加注释
// 2019/10/11: 修改pragma once和include
// 2020/1/14: 接口完整性
// 2020/11/12: 支持Spadas7

#ifndef FREESPACE_SAMPLE_V1_H
#define FREESPACE_SAMPLE_V1_H

#include "spadas.h"

namespace freespace_sample_v1
{
	using namespace spadas;

#if SPADAS_VERSION_MAJOR < 7
	typedef OptionalDouble GeneralElement;
#endif

	struct FreespacePointSequence
	{
		Array<Point2D> points; // 每段可行驶区域的顶点列表，单位m
	};

	struct FreespaceSample
	{
		GlobalTimestamp timeStamp; // 时间戳
		Array<FreespacePointSequence> sequences; // 可行驶区域列表(可多段描述)

		FreespaceSample()
		{}

		static String getProtocolName(UInt channel)
		{
			if (channel == UINF) return "freespace-sample-v1";
			else return (String)"freespace-sample-v1@" + channel;
		}

		static String getTitle()
		{
			return "Sequence count,First seq's offset, First seq's point count,etc";
		}

		static Bool supportInterpolation()
		{
			return FALSE;
		}

		GeneralSample toGeneralSample()
		{
			GeneralSample sample;
			sample.timeStamp = timeStamp;

			UInt pointCount = 0;
			for (UInt i = 0; i < sequences.size(); i++)
			{
				pointCount += sequences[i].points.size();
			}

			sample.values = Array<GeneralElement>(1 + 2 * (sequences.size() + pointCount));
			GeneralElement *data = sample.values.data();

			UInt seqBase = 1;
			UInt ptBase = seqBase + 2 * sequences.size();

			data[0] = sequences.size();

			pointCount = 0;
			for (UInt i = 0; i < sequences.size(); i++)
			{
				Array<Point2D> pts = sequences[i].points;

				data[seqBase + 2 * i] = pointCount;
				data[seqBase + 2 * i + 1] = pts.size();

				for (UInt n = 0; n < pts.size(); n++)
				{
					data[ptBase + 2 * (pointCount + n)] = pts[n].x;
					data[ptBase + 2 * (pointCount + n) + 1] = pts[n].y;
				}

				pointCount += pts.size();
			}

			sample.significantCount = sample.values.size();
			return sample;
		}

		Bool fromGeneralSample(GeneralSample sample)
		{
			// 未实现
			return FALSE;
		}

		Bool fromGeneralSample(String protocol, GeneralSample sample)
		{
			// 未实现
			return FALSE;
		}

		static FreespaceSample interpolate(FreespaceSample& s1, Double w1, FreespaceSample& s2, Double w2, GlobalTimestamp time)
		{
			return FreespaceSample();
		}
	};
}

#endif